#include "StdAfx.h"
#include "Shape2D.h"


Shape2D::Shape2D(void)
{
	this->vertexes.clear();
}


Shape2D::Shape2D(std::vector<Point2D>& vertexesList)
{
	this->vertexes.clear();
	this->vertexes = vertexesList;
}

Shape2D::~Shape2D(void)
{
	this->vertexes.clear();
}

Point2D* Shape2D::GetCenter()
{
	int n = this->vertexes.size();
	if(n > 0)
	{
		Point2D p = Point2D();
		for(int i = 0; i < n; ++i)
		{
			p += this->vertexes[i];
		}
		p /= n;
		return &p;
	}
	return nullptr;
}

void Shape2D::ComputeAABB()
{
	this->maxAABB = Point2D();
	this->minAABB = Point2D();
	if(this->vertexes.size() > 0)
	{
		this->maxAABB = Point2D(INT_MIN);
		this->minAABB = Point2D(INT_MAX);
		for(int i = 0; i < this->vertexes.size(); ++i)
		{
			this->minAABB = Point2D::min(this->minAABB, this->vertexes[i]);
			this->maxAABB = Point2D::max(this->maxAABB, this->vertexes[i]);
		}
	}
}

int Shape2D::Area() const
{
	Point2D len = this->maxAABB - this->minAABB;
	return len.GetX() * len.GetY();
}